﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IrisGenerator.Framework.UI.Controls;
using IrisGenerator.Framework.Reflection;

namespace IrisGenerator.Framework.UI.Controls
{
    public class BindableComboBox:ComboBox,IBindableControl
    {
        public BindableComboBox()
            : base()
        {
            this.Load += new EventHandler(ComboBoxBase_Load);
        }

        void ComboBoxBase_Load(object sender, EventArgs e)
        {
            this.LoadSource();
        }

        public event EventHandler Load;

        private bool _calledOnLoad;

        protected object InnerSource { get; private set; }

        protected void OnLoad(EventArgs e)
        {
            if (this.Load != null)
                this.Load(this, e);
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (!_calledOnLoad)
            {
                this._calledOnLoad = true;
                this.OnLoad(EventArgs.Empty);
            }
        }

        public void LoadSource()
        {
            if (this.SourceMethod != null && !this.DesignMode)
                this.DataSource = GetDataSourceFromMethod();             
            //return this.InnerSource;
        }

        private object GetDataSourceFromMethod()
        {
            bool result;
            object tmp=SourceMethod.Invoke(this,out result);
            return tmp;
        }

        public MethodCall SourceMethod { get; set; }

    }
}
